package com.epalmpay.controller.machinewasher;


import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.machinewasher.MachineWasherInDTO;
import com.epalmpay.dto.machinewasher.MachineWasherOutDTO;
import com.epalmpay.dto.machinewasher.MachineWasherOutDTOList;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.machinewasher.IMachineWasherService;
import com.epalmpay.service.management.ITemplateMailSendService;
import com.epalmpay.service.trade.ITradeService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/3/15.
 */
@Controller
@RequestMapping(value="channel/machineWasher")
public class MachineWasherController extends BaseController {

    @Resource
    private IMachineWasherService machineWasherService;

    @Autowired
    @Lazy
    private ITemplateMailSendService templateMailSendService;

    @Resource
    private ITradeService tradeService;


    @ResponseBody
    @RequestMapping(value = "/list")
    public MachineWasherOutDTOList list(MachineWasherInDTO machineWasherInDTO, PageDTO pageDTO)throws Exception{
        MachineWasherOutDTOList machineWasherOutDTOList = new MachineWasherOutDTOList();
        try {
            Page page= Tools.pageEmtity(pageDTO);
            machineWasherOutDTOList= machineWasherService.queryMachineWasherList(machineWasherInDTO,page);

            machineWasherOutDTOList.setErrorCode(Rescode.SUCCESS, "处理成功");

            machineWasherOutDTOList.setPage(page);
            return machineWasherOutDTOList;
        } catch (ApplicationException ae) {
            machineWasherOutDTOList.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            machineWasherOutDTOList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return machineWasherOutDTOList;

    }
    @ResponseBody
    @RequestMapping(value = "/updateAddress")
    public MachineWasherOutDTO updateAddress(MachineWasherInDTO machineWasherInDTO)throws Exception{
        MachineWasherOutDTO machineWasherOutDTO = new MachineWasherOutDTO();
        try {

            machineWasherService.updateAddress(machineWasherInDTO);

            machineWasherOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

            return machineWasherOutDTO;
        } catch (ApplicationException ae) {
            machineWasherOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            machineWasherOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return machineWasherOutDTO;

    }
    @ResponseBody
    @RequestMapping(value = "/editWash")
    public MachineWasherOutDTO editWash(MachineWasherInDTO machineWasherInDTO)throws Exception{
        MachineWasherOutDTO machineWasherOutDTO = new MachineWasherOutDTO();
        try {

            machineWasherService.editWash(machineWasherInDTO);

            machineWasherOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

            return machineWasherOutDTO;
        } catch (ApplicationException ae) {
            machineWasherOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            machineWasherOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return machineWasherOutDTO;

    }

    @ResponseBody
    @RequestMapping(value = "/refundOrder")
    public Map refundOrder(HttpServletRequest request) {
        Map result = new HashMap();
        String orderno = request.getParameter("orderno");
        String memberid = request.getParameter("memberid");
        TradeOffline to = new TradeOffline();
        to.setOrderId(orderno);
        try {
            machineWasherService.refundOrder(to);
            TradeOffline tradeOffline = tradeService.selectByOrderId(orderno);
            //微信发送模板消息
            if(tradeOffline.getPayerId()!=null){
                try {
                    templateMailSendService.sendTemplate(tradeOffline,  BizEnum.WebTemplateType.template5.getType(),
                            tradeOffline.getPayerId(),
                            tradeOffline.getGroupId(), BizEnum.PaymentType.WebChat.getType(),ROOT_HOME_URL);
                } catch (Exception e) {
                    logger.info ("模板消息发送失败"+e);
                }
            }
            result.put("result", "000000");
        }catch (Exception e) {
            e.printStackTrace();
            result.put("result", "000001");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/updateMachineWasherByShortId")
    public Map updateMachineWasherByShortId(HttpServletRequest request) {
        Map result = new HashMap();

        Map param = new HashMap();
        param.put("shortid", request.getParameter("shortid"));
        param.put("address", request.getParameter("address"));
        int up = machineWasherService.updateMachineWasherByShortId(param);

        result.put("result", "0000");
        result.put("code", up);
        return result;
    }


    @ResponseBody
    @RequestMapping(value = "/queryWashDetail")
    public MachineWasherOutDTOList queryWashDetail(MachineWasherInDTO machineWasherInDTO,PageDTO pageDTO)throws Exception{
        MachineWasherOutDTOList machineWasherOutDTOList = new MachineWasherOutDTOList();
        Page page= Tools.pageEmtity(pageDTO);
        try {
            machineWasherOutDTOList= machineWasherService.queryWashDetail(machineWasherInDTO,page);

            machineWasherOutDTOList.setErrorCode(Rescode.SUCCESS, "处理成功");

            machineWasherOutDTOList.setPage(page);
            return machineWasherOutDTOList;
        } catch (ApplicationException ae) {
            machineWasherOutDTOList.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            machineWasherOutDTOList.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return machineWasherOutDTOList;

    }

    @ResponseBody
    @RequestMapping(value = "/batchEditWashBelong")
    public MachineWasherOutDTO batchEditWashBelong(MachineWasherInDTO machineWasherInDTO)throws Exception{
        MachineWasherOutDTO machineWasherOutDTO = new MachineWasherOutDTO();
        try {

            machineWasherService.batchEditWashBelong(machineWasherInDTO);
            machineWasherOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

            return machineWasherOutDTO;
        } catch (ApplicationException ae) {
            machineWasherOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        }
        catch (Exception e) {
            machineWasherOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return machineWasherOutDTO;

    }



}
